package com.me.study.javaCore.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * JDBC：MySQL
 *
 * @author ME
 * @date 2021/4/1
 */
public class MySqlUtil {

    public static void main(String[] args) {
        exeBatchParparedSQL();
    }

    private static final String URL = "jdbc:mysql://61.141.222.103:3306/test-db";
    private static final String USER = "root";
    private static final String PASSWORD = "Cmck123$%^";

    public static Connection getConnection() {
        try {
            return DriverManager.getConnection(URL, USER, PASSWORD);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public static void exeBatchParparedSQL() {
        String sql = "UPDATE sys_user SET age=? WHERE ID = ?";

        try (Connection conn = getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            conn.setAutoCommit(false);
            pstmt.setInt(1, 999);
            pstmt.setInt(2, 20);
            pstmt.addBatch();
            pstmt.setInt(1, 20);
            pstmt.setInt(2, 20);
            pstmt.addBatch();

            pstmt.executeBatch();

            conn.commit();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}
